package com.tdam2014.grupo3.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.tdam2014.grupo3.entities.People;

public class PeopleDatabaseHelper extends DatabaseHelper {

	public PeopleDatabaseHelper(Context context) {
		super(context);
	}
	
	public ArrayList<People> getPeopleList() {
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM " + tblPeople + " ORDER BY " + colPeopleRealname + " COLLATE NOCASE ASC";
		Cursor cursor = db.rawQuery(query, null);
		
		People people;
		ArrayList<People> peoples = new ArrayList<People>();
		while (cursor.moveToNext()) {
			people = new People();
			people.setNsid(cursor.getString(cursor.getColumnIndex(colPeopleNSID)));
			people.setUsername(cursor.getString(cursor.getColumnIndex(colPeopleUsername)));
			people.setRealName(cursor.getString(cursor.getColumnIndex(colPeopleRealname)));
			people.setIconServer(cursor.getInt(cursor.getColumnIndex(colPeopleIconserver)));
			people.setIconFarm(cursor.getInt(cursor.getColumnIndex(colPeopleIconfarm)));
			people.setCountPhotoSets(cursor.getInt(cursor.getColumnIndex(colPeopleCountPhotoSets)));
			
			peoples.add(people);
		}
		
		return peoples;
	}
	
	public People getPeopleByUsername(String username) {
		SQLiteDatabase db = this.getReadableDatabase();
		
		Cursor cursor = db.query(tblPeople, null, colPeopleUsername + "= ?", new String[] {username}, null, null, null);
		
		if(cursor.moveToFirst()) {
			People people = new People();
			people.setNsid(cursor.getString(cursor.getColumnIndex(colPeopleNSID)));
			people.setUsername(cursor.getString(cursor.getColumnIndex(colPeopleUsername)));
			people.setRealName(cursor.getString(cursor.getColumnIndex(colPeopleRealname)));
			people.setIconServer(cursor.getInt(cursor.getColumnIndex(colPeopleIconserver)));
			people.setIconFarm(cursor.getInt(cursor.getColumnIndex(colPeopleIconfarm)));
			
			return people;
		}
		
		return null;
	}
	
	public void addPeople(People people) {
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues cv = new ContentValues();
		cv.put(colPeopleNSID, people.getNsid());
		cv.put(colPeopleUsername, people.getUsername());
		cv.put(colPeopleRealname, people.getRealName());
		cv.put(colPeopleIconserver, people.getIconServer());
		cv.put(colPeopleIconfarm, people.getIconFarm());
		
		db.insert(tblPeople, null, cv);
		db.close();
	}
	
	public void deletePeople(String nsid) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(tblPeople, colPeopleNSID + "=?", new String [] {nsid});
		db.close();
	}
	
	public int getPeopleCountPhotoSets(String nsid) {
		SQLiteDatabase db = this.getReadableDatabase();
		
		Cursor cursor = db.query(tblPeople, new String[] {colPeopleCountPhotoSets}, colPeopleNSID + "=?", new String[] {nsid}, null, null, null);
		if(cursor.moveToFirst()) return cursor.getInt(cursor.getColumnIndex(colPeopleCountPhotoSets));
		else return -1;
	}
	
	public void updatePeopleCountPhotoSets(String nsid, int countPhotoSets) {
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues cv = new ContentValues();
		cv.put(colPeopleCountPhotoSets, countPhotoSets);
		
		db.update(tblPeople, cv, colPeopleNSID + "=?", new String [] {nsid});
		db.close();
	}
}
